﻿using ProjScan.Tools;
using System.Text;
using UnityEngine;

namespace ProjScan.RecordManager.AssetData
{
    internal class ParticleSystemAssetData : ComponentAssetData<ParticleSystem>
    {
        public int textureArrayLength;

        public int maxTextureSize;

        public int maxParticles;

        public int meshTriangles;

        public string shaderName;

        public ParticleSystemRenderMode renderMode;

        public bool isMeshReadable;

        public bool isCollisionEnabled;

        public bool isTriggerEnabled;

        public bool isPrewarmed;

        public ParticleSystemAssetData(int id, ParticleSystem particleSystem, string name)
            : base(id, name, particleSystem)
        {
            isCollisionEnabled = false;
            isTriggerEnabled = false;
            isPrewarmed = false;
            ParticleSystemRenderer component = particleSystem.GetComponent<ParticleSystemRenderer>();
            maxTextureSize = 0;
            textureArrayLength = 0;
            meshTriangles = 0;
            renderMode = ParticleSystemRenderMode.None;
            isMeshReadable = true;
            isCollisionEnabled = particleSystem.collision.enabled;
            isTriggerEnabled = particleSystem.trigger.enabled;
            if (component != null)
            {
                Texture[] array = MaterialAssetData.MaterialTextures(component.sharedMaterial);
                maxTextureSize = DetermineMaxTextureSize(array);
                meshTriangles = CalculateMeshTriangles(component);
                if (component.sharedMaterial != null)
                {
                    shaderName = component.sharedMaterial.shader.name;
                }
                else
                {
                    shaderName = string.Empty;
                }

                renderMode = component.renderMode;
                textureArrayLength = array.Length;
                if (renderMode == ParticleSystemRenderMode.Mesh && component.mesh != null)
                {
                    isMeshReadable = component.mesh.isReadable;
                }
            }

            object obj = null;
            object obj2 = null;
            object propertyValue = AssemblyTools.I.GetPropertyValue(particleSystem, "main");
            if (propertyValue != null)
            {
                obj = AssemblyTools.I.GetPropertyValue(propertyValue, "maxParticles");
                obj2 = AssemblyTools.I.GetPropertyValue(propertyValue, "prewarm");
            }

            if (obj == null)
            {
                obj = AssemblyTools.I.GetPropertyValue(particleSystem, "maxParticles");
            }

            if (obj2 == null)
            {
                obj2 = AssemblyTools.I.GetPropertyValue(particleSystem, "prewarm");
            }

            maxParticles = ((obj != null) ? ((int)obj) : 0);
            isPrewarmed = (obj2 != null && (bool)obj2);
        }

        private int DetermineMaxTextureSize(Texture[] textureArray)
        {
            int num = 0;
            int num2 = 0;
            foreach (Texture texture in textureArray)
            {
                if (texture != null)
                {
                    num2 = ((texture.width > texture.height) ? texture.width : texture.height);
                    num = ((num2 > num) ? num2 : num);
                }
            }

            return num;
        }

        private int CalculateMeshTriangles(ParticleSystemRenderer renderer)
        {
            if (renderer.renderMode != ParticleSystemRenderMode.Mesh || renderer.mesh == null)
            {
                return -1;
            }

            return renderer.mesh.triangles.Length / 3;
        }

        public override string GetCSVHeader()
        {
            return "Name,DirID,MaxParticle,RenderMod,MaxTexture,MeshTriangle";
        }

        public override string GetInfo()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(ToolsHelper.QuoteString(name));
            stringBuilder.Append(',');
            stringBuilder.Append(directoryId);
            stringBuilder.Append(',');
            stringBuilder.Append(maxParticles);
            stringBuilder.Append(',');
            stringBuilder.Append(renderMode.ToString());
            stringBuilder.Append(',');
            stringBuilder.Append(maxTextureSize);
            stringBuilder.Append(',');
            stringBuilder.Append(meshTriangles);
            return stringBuilder.ToString();
        }
    }
}